var timerLoadAluno;

$(function() {
    var textToSpeech = $($('#tbResult tbody tr:first td')[1]).html()+'%2C'+' mesa '+$($('#tbResult tbody tr:first td')[3]).html();
    textToSpeech = textToSpeech.split(' ').join('+').toLowerCase();
    jwplayer('player').setup({
        'flashplayer': '/siscomvest/js/mediaplayer-5.9/player.swf',
        'playlist': [
                { file: '/siscomvest/js/mediaplayer-5.9/song.mp3'},
                { file: '/siscomvest/exibirSenha/audio?q='+textToSpeech}
            ],
        'controlbar': 'bottom',
        'width': '470',
        'height': '1',
        'autostart': 'true',
        'repeat': 'list',
        'events': {
            onComplete: function() {
                timerLoadAluno = setInterval("loadNextAluno()",3000);
            }, 
            onPlay: function(){
                clearTimeout(timerLoadAluno);
            }
        }
    });
    loadDataHora();
    setInterval("loadDataHora()",60000);
    $('#conteudo').height($('#conteudo').height()-1);
});


function loadDataHora(){
    $.ajax({
        type: 'get',
        url: "loadDataHora",
        data: '',
        success: function(data){
            $('#datahora').html(data);
        }
     });
}

function loadNextAluno(){
    $.ajax({
        type: 'post',
        url: "loadNextAluno",
        beforeSend: function(){

        },
        success: function(data){
            if(data.length>0 && data[0].id==$('#tbResult tbody tr:first').attr('id')){
                return false;
            }
            var trHtml = '';
            var textToSpeech = '';
            $(data).each(function(){
                if(trHtml == ''){
                    textToSpeech = this.aluno+'%2C'+' mesa '+this.mesa;
                    textToSpeech = textToSpeech.split(' ').join('+').toLowerCase();
                }
                trHtml += '<tr id="'+this.id+'"><td>'+this.colocacao+'.</td><td>'+this.aluno+'</td><td> - </td><td>'+this.mesa+'</td></tr>';                
            });
            $('#tbResult tbody').html(trHtml);
            jwplayer().load([
                { file: '/siscomvest/js/mediaplayer-5.9/song.mp3'},
                { file: '/siscomvest/exibirSenha/audio?q='+textToSpeech}
            ]);
            jwplayer().playlistItem(0);
            jwplayer().play(true);
        }
    });
}